/**
 * @filename:UserServiceImpl 2019年4月9日
 * @project user_center  V1.0
 * Copyright(c) 2018 BianPeng Co. Ltd. 
 * All right reserved. 
 */
package com.association.service.impl;

import com.alibaba.excel.EasyExcel;
import com.association.constant.CodeEnum;
import com.association.mapper.UserAwaitMapper;
import com.association.model.ResultResp;
import com.association.model.entity.User;
import com.association.model.entity.UserAwait;
import com.association.model.vo.UserAwaitVo;
import com.association.service.UserAwaitService;
import com.association.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.len.base.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**   
 *  
 * @Description:  用户——SERVICEIMPL
 * @Author:       BianPeng   
 * @CreateDate:   2019年4月9日
 * @Version:      V1.0
 *    
 */
@Service
public class UserAwaitServiceImpl extends BaseServiceImpl<UserAwait, String> implements UserAwaitService {

    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public ResultResp select(Integer pageNum,Integer pageSize,String nickName,String phone,String deptId,String sex,String email) {
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=10;
        }

        QueryWrapper<UserAwait> wrapper = new QueryWrapper<>();
//        if (StringUtils.isEmpty(nickName)){
//
//        }else if(StringUtils.isEmpty(userAwaitVo.getUserCode())){
//            wrapper.eq("userCode",userAwaitVo.getUserCode());
//        }else if(StringUtils.isEmpty(userAwaitVo.getSex())){
//            wrapper.eq("sex",userAwaitVo.getSex());
//        }else if(StringUtils.isEmpty(userAwaitVo.getCreateYear())){
//            wrapper.eq("createYear",userAwaitVo.getCreateYear());
//        }else if(StringUtils.isEmpty(userAwaitVo.getCollege())){
//            wrapper.eq("college",userAwaitVo.getCollege());
//        }else if(StringUtils.isEmpty(userAwaitVo.getSpecialty())){
//            wrapper.eq("specialty",userAwaitVo.getSpecialty());
//        }else if(StringUtils.isEmpty(userAwaitVo.getRemark())){
//            wrapper.eq("remark",userAwaitVo.getRemark());
//        }else if(StringUtils.isEmpty(userAwaitVo.getState())){
//            wrapper.eq("state",userAwaitVo.getState());
//        }
        return new ResultResp(PageUtil.listToPage(this.list(wrapper),pageNum,pageSize).getRecords(),this.count(wrapper));
    }

    @Override
    public ResultResp delete(UserAwaitVo userAwaitVo) {
        if (this.removeById(userAwaitVo.getId())){
            return new ResultResp(CodeEnum.SUCCESS);
        }
        return new ResultResp(CodeEnum.SERVER);
    }

    @Override
    public ResultResp deleteIds(List<Long> ids) {
        if (!ids .isEmpty()&&ids.size()==0) {
            return new ResultResp(CodeEnum.SERVER);
        }else{
            QueryWrapper<UserAwait> wrapper = new QueryWrapper<>();
            wrapper.in("id",ids);

            if (this.remove(wrapper)){
                return new ResultResp(CodeEnum.SUCCESS);
            }
            return new ResultResp(CodeEnum.SERVER);
        }

    }

    @Override
    public ResultResp edit(UserAwaitVo userAwaitVo) {
        if (this.update(convertToUserAwait(userAwaitVo),new QueryWrapper<UserAwait>().eq("id",userAwaitVo.getId()))){
            return new ResultResp(CodeEnum.SUCCESS);
        }
        return new ResultResp(CodeEnum.SERVER);
    }

    @Override
    public ResultResp add(UserAwaitVo userAwaitVo) {
        if (this.save(convertToUserAwait(userAwaitVo))){
            return new ResultResp(CodeEnum.SUCCESS);
        }
        return new ResultResp(CodeEnum.SERVER);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultResp addList(MultipartFile file) {
        try{
            int num = 0;
            List<UserAwaitVo> memberList = EasyExcel.read(file.getInputStream())
                    .head(UserAwaitVo.class)
                    .sheet()
                    .doReadSync();
            for (UserAwaitVo userAwait : memberList) {
                if (StringUtils.isEmpty(userAwait.getUserCode())){
                    continue;
                }
                if(this.save(convertToUserAwait(userAwait))){
                    num+=1;
                }
            }
            return new ResultResp("200",String.format(String.valueOf(CodeEnum.UNACCESS.getMessage()),num,memberList.size()-num));
        }catch (Exception e){
            logger.error(String.format("面试名单导入失败:%s", e.getMessage()));
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultResp(CodeEnum.EXCELSERVER);
        }
    }
    /**
     * 判断对象中属性值是否全为空
     *
     * @param userAwaitVo
     * @return
     */
    public static UserAwait convertToUserAwait(UserAwaitVo userAwaitVo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return UserAwait.builder()
                .userCode(userAwaitVo.getUserCode())
                .name(userAwaitVo.getName())
                .college(userAwaitVo.getCollege())
                .specialty(userAwaitVo.getSpecialty())
                .schoolClass(userAwaitVo.getSchoolClass())
                .mail(userAwaitVo.getMail())
                .state("1")
                .feature(userAwaitVo.getFeature())
                .createYear(sdf.format(date))
                .phone(userAwaitVo.getPhone())
                .sex(userAwaitVo.getSex())
                .remark(userAwaitVo.getRemark())
                .build();
    }
}